/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.annotated;

import junit.framework.AssertionFailedError;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.sourceprojects.lycia.test.TestCaseUtilities;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;

public class IntegerWrapperConvertTestCase {

	private static final String XML_SIMPLE = TestCaseUtilities.XML_HEADER
			+ "<test><testcase-simple value=\"100\" /></test>";
	private static final String XML_DEFAULT = TestCaseUtilities.XML_HEADER
			+ "<test><testcase-default /></test>";

	@Test
	public void testSimpleIntegerConvert() throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.parser(
				tag("testcase-simple", method("simpleIntegerConvert", this,
						Integer.class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test(expected = LyciaParsingException.class)
	public void testDefaultIntegerConvertNullAware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(true)).parser(
				tag("testcase-default", method("defaultIntegerConvert", this,
						Integer.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testDefaultIntegerConvertNullUnaware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false))
				.parser(
						tag("testcase-default", method("defaultIntegerConvert",
								this, Integer.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testGivenDefaultIntegerConvertNullAware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(true)).parser(
				tag("testcase-default", method("givenDefaultIntegerConvert",
						this, Integer.class))).build();

		parser.parse(XML_DEFAULT);
	}

	@Test
	public void testGivenDefaultIntegerConvertNullUnaware()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.parser(
				tag("testcase-default", method("givenDefaultIntegerConvert",
						this, Integer.class))).build();

		parser.parse(XML_DEFAULT);
	}

	public void simpleIntegerConvert(@Attribute("value") Integer value) {
		if (value != 100)
			throw new AssertionFailedError("Expected 100 but was " + value);
	}

	public void defaultIntegerConvert(@Attribute("value") Integer value) {
		if (value != null)
			throw new AssertionFailedError("Expected null but was " + value);
	}

	public void givenDefaultIntegerConvert(
			@Attribute(value = "value", defaultValue = "50") Integer value) {

		if (value != 50)
			throw new AssertionFailedError("Expected 50 but was " + value);
	}

}
